﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class EquipmentTypeDA : IEquipmentTypeDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string CUBIC_CAPACITY = "@CUBIC_CAPACITY";
        private string DESCR = "@DESCR";
        private string EQUIPMENT_TYPE_ID = "@EQUIPMENT_TYPE_ID";
        private string HEIGHT = "@HEIGHT";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string LENGTH = "@LENGTH";
        private string SQL_CHECK_EQUIPMENT_TYPE_ID_UNIQUE = " SELECT COUNT(1) FROM @_@EQUIPMENT_TYPE WHERE EQUIPMENT_TYPE_ID = @EQUIPMENT_TYPE_ID ";
        private string SQL_DELETE_EQUIPMENT_TYPE = " DELETE FROM @_@EQUIPMENT_TYPE WHERE EQUIPMENT_TYPE_ID = @EQUIPMENT_TYPE_ID ";
        private string SQL_INSERT_EQUIPMENT_TYPE = " INSERT INTO @_@EQUIPMENT_TYPE ( WH_ID, EQUIPMENT_TYPE_ID, DESCR, CUBIC_CAPACITY, WEIGHT_CAPACITY, LENGTH, WIDTH, HEIGHT, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @EQUIPMENT_TYPE_ID, @DESCR, @CUBIC_CAPACITY, @WEIGHT_CAPACITY, @LENGTH, @WIDTH, @HEIGHT, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_EQUIPMENT_TYPE = " SELECT  ROWID, WH_ID, EQUIPMENT_TYPE_ID, DESCR, CUBIC_CAPACITY, WEIGHT_CAPACITY, LENGTH, WIDTH, HEIGHT, IS_ACTIVE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@EQUIPMENT_TYPE ";
        private string SQL_UPDATE_EQUIPMENT_TYPE = " UPDATE @_@EQUIPMENT_TYPE SET WH_ID = @WH_ID, DESCR = @DESCR, CUBIC_CAPACITY = @CUBIC_CAPACITY, WEIGHT_CAPACITY = @WEIGHT_CAPACITY, LENGTH = @LENGTH, WIDTH = @WIDTH, HEIGHT = @HEIGHT, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE EQUIPMENT_TYPE_ID = @EQUIPMENT_TYPE_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WEIGHT_CAPACITY = "@WEIGHT_CAPACITY";
        private string WH_ID = "@WH_ID";
        private string WIDTH = "@WIDTH";

        public bool CheckEquipmentTypeIDUnique(string equipmentTypeID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@EQUIPMENT_TYPE_ID", equipmentTypeID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_EQUIPMENT_TYPE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckEquipmentTypeIDUnique(DataBase dataBase, DbTransaction tran, string equipmentTypeID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@EQUIPMENT_TYPE_ID", equipmentTypeID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_EQUIPMENT_TYPE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteEquipmentType(List<string> equipmentTypeID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < equipmentTypeID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@EQUIPMENT_TYPE_ID", equipmentTypeID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_EQUIPMENT_TYPE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteEquipmentType(DataBase dataBase, DbTransaction tran, List<string> equipmentTypeID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < equipmentTypeID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@EQUIPMENT_TYPE_ID", equipmentTypeID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_EQUIPMENT_TYPE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllEquipmentType(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_EQUIPMENT_TYPE.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllEquipmentType(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_EQUIPMENT_TYPE.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public EquipmentTypeInfo GetEquipmentTypeByID(string equipmenttypeID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_EQUIPMENT_TYPE.Replace("@_@", whLoginID) + " WHERE EQUIPMENT_TYPE_ID = @EQUIPMENT_TYPE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@EQUIPMENT_TYPE_ID", equipmenttypeID) };
            EquipmentTypeInfo equipmentTypeInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    equipmentTypeInfo = new EquipmentTypeInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["EQUIPMENT_TYPE_ID"].ToString(), reader["DESCR"].ToString(), (reader["CUBIC_CAPACITY"].ToString() != "") ? decimal.Parse(reader["CUBIC_CAPACITY"].ToString()) : 0M, (reader["WEIGHT_CAPACITY"].ToString() != "") ? decimal.Parse(reader["WEIGHT_CAPACITY"].ToString()) : 0M, (reader["LENGTH"].ToString() != "") ? decimal.Parse(reader["LENGTH"].ToString()) : 0M, (reader["WIDTH"].ToString() != "") ? decimal.Parse(reader["WIDTH"].ToString()) : 0M, (reader["HEIGHT"].ToString() != "") ? decimal.Parse(reader["HEIGHT"].ToString()) : 0M, reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return equipmentTypeInfo;
        }

        public EquipmentTypeInfo GetEquipmentTypeByID(DataBase dataBase, DbTransaction tran, string equipmenttypeID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_EQUIPMENT_TYPE.Replace("@_@", whLoginID) + " WHERE EQUIPMENT_TYPE_ID = @EQUIPMENT_TYPE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@EQUIPMENT_TYPE_ID", equipmenttypeID) };
            EquipmentTypeInfo equipmentTypeInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                equipmentTypeInfo = new EquipmentTypeInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["EQUIPMENT_TYPE_ID"].ToString(), reader["DESCR"].ToString(), (reader["CUBIC_CAPACITY"].ToString() != "") ? decimal.Parse(reader["CUBIC_CAPACITY"].ToString()) : 0M, (reader["WEIGHT_CAPACITY"].ToString() != "") ? decimal.Parse(reader["WEIGHT_CAPACITY"].ToString()) : 0M, (reader["LENGTH"].ToString() != "") ? decimal.Parse(reader["LENGTH"].ToString()) : 0M, (reader["WIDTH"].ToString() != "") ? decimal.Parse(reader["WIDTH"].ToString()) : 0M, (reader["HEIGHT"].ToString() != "") ? decimal.Parse(reader["HEIGHT"].ToString()) : 0M, reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return equipmentTypeInfo;
        }

        public DataSet GetEquipmentTypeByQueryList(List<string> sqlWhere, EquipmentTypeQueryEntity equipmenttypeQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_EQUIPMENT_TYPE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!equipmenttypeQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, equipmenttypeQuery.CurrentPage, equipmenttypeQuery.PageSize, equipmenttypeQuery.SortField, equipmenttypeQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetEquipmentTypeByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, EquipmentTypeQueryEntity equipmenttypeQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_EQUIPMENT_TYPE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, equipmenttypeQuery.CurrentPage, equipmenttypeQuery.PageSize, equipmenttypeQuery.SortField, equipmenttypeQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertEquipmentType(EquipmentTypeInfo equipmentTypeInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    equipmentTypeInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_EquipmentType_Parameters(equipmentTypeInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_EQUIPMENT_TYPE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertEquipmentType(DataBase dataBase, DbTransaction tran, EquipmentTypeInfo equipmentTypeInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    equipmentTypeInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_EquipmentType_Parameters(equipmentTypeInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_EQUIPMENT_TYPE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_EquipmentType_Parameters(EquipmentTypeInfo equipmentTypeInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.EQUIPMENT_TYPE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.DESCR, SqlDbType.VarChar, 100), new SqlParameter(this.CUBIC_CAPACITY, SqlDbType.Decimal, 13), new SqlParameter(this.WEIGHT_CAPACITY, SqlDbType.Decimal, 13), new SqlParameter(this.LENGTH, SqlDbType.Decimal, 13), new SqlParameter(this.WIDTH, SqlDbType.Decimal, 13), new SqlParameter(this.HEIGHT, SqlDbType.Decimal, 13), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(equipmentTypeInfo.WhID))
            {
                paramArray[0].Value = equipmentTypeInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = equipmentTypeInfo.EquipmentTypeID;
            if (!string.IsNullOrEmpty(equipmentTypeInfo.Descr))
            {
                paramArray[2].Value = equipmentTypeInfo.Descr;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            paramArray[3].Value = equipmentTypeInfo.CubicCapacity;
            paramArray[4].Value = equipmentTypeInfo.WeightCapacity;
            paramArray[5].Value = equipmentTypeInfo.Length;
            paramArray[6].Value = equipmentTypeInfo.Width;
            paramArray[7].Value = equipmentTypeInfo.Height;
            paramArray[8].Value = equipmentTypeInfo.IsActive;
            paramArray[9].Value = equipmentTypeInfo.CreatedBy;
            paramArray[10].Value = equipmentTypeInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateEquipmentType(EquipmentTypeInfo equipmentTypeInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    equipmentTypeInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_EquipmentType_Parameters(equipmentTypeInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_EQUIPMENT_TYPE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateEquipmentType(DataBase dataBase, DbTransaction tran, EquipmentTypeInfo equipmentTypeInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    equipmentTypeInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_EquipmentType_Parameters(equipmentTypeInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_EQUIPMENT_TYPE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

